home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 408_01 / post.c < prev    next >
Text File  |  1993-08-06  |  10KB  |  373 lines

  1. /*
  2.     SNEWS 1.91
  3.  
  4.     post - news posting stuff
  5.  
  6.  
  7.     Copyright (C) 1991  John McCombs, Christchurch, NEW ZEALAND
  8.                         john@ahuriri.gen.nz
  9.                         PO Box 2708, Christchurch, NEW ZEALAND
  10.  
  11.     Modifications copyright (C) 1993  Daniel Fandrich
  12.                         <dan@fch.wimsey.bc.ca> or CompuServe 72365,306
  13.  
  14.     This program is free software; you can redistribute it and/or modify
  15.     it under the terms of the GNU General Public License, version 1, as
  16.     published by the Free Software Foundation.
  17.  
  18.     This program is distributed in the hope that it will be useful,
  19.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.     GNU General Public License for more details.
  22.  
  23.     See the file COPYING, which contains a copy of the GNU General
  24.     Public License.
  25.  
  26.  
  27.     Source is formatted with a tab size of 4.
  28.  
  29.  */
  30.  
  31.  
  32. #include "defs.h"
  33. #include "snews.h"
  34. #include <alloc.h>
  35. #include <ctype.h>
  36. #include <io.h>
  37. #include <time.h>
  38.  
  39.  
  40. /*------------------------------- post an article --------------------------*/
  41. void post(TEXT *tx, char *gx, char *subject)
  42. {
  43.     /*
  44.      *  Ask for the data we are not given in the arguments,
  45.      *  let him edit the article, then post it
  46.      */
  47.  
  48.     FILE *article, *sig;
  49.     LINE *ln;
  50.     char buf[129];
  51.     char newsgroups[256];
  52.     char dist[80];
  53.     char sig_fn[80];
  54.     char author[WHO_LENGTH], msg_id[MSG_ID_LENGTH];
  55.     char tempfile[81];
  56.     int  ch;
  57.  
  58.     strcpy(newsgroups, gx);
  59.  
  60.     strcpy(author, "");
  61.     strcpy(msg_id, "");
  62.  
  63.     sprintf(tempfile, "%spost%04x.tmp", my_stuff.temp_str, getpid());
  64.     if ((article = fopen(tempfile, "w+")) != NULL) {
  65.  
  66.         command("");
  67.         if (strlen(newsgroups) == 0) {
  68.             /* if no groups ask for them */
  69.             lmessage("Newsgroups? ");
  70.             gets(newsgroups);
  71.  
  72.         } else {
  73.  
  74.             /* there is a group ask if he wants to change */
  75.             sprintf(buf, "Group: %s; post to *this* group (y/n)?", newsgroups);
  76.             message(buf);
  77.             while (((ch = tolower(getch())) != 'y') && (ch != 'n'));
  78.  
  79.             if (ch == 'n') {
  80.                 lmessage("Newsgroups? ");
  81.                 gets(newsgroups);
  82.             }
  83.  
  84.         }
  85.  
  86.         /* check for a valid newsgroups line */
  87.         if (newsgroups_valid(newsgroups)) {
  88.  
  89.             if (strlen(subject) != 0) {        /* Subject exists */
  90.                 message("Change Subject (y/n)? ");
  91.                 while (((ch = tolower(getch())) != 'y') && (ch != 'n'));
  92.                 if (ch == 'y') {
  93.                    lmessage("Subject: ");
  94.                    gets(subject);
  95.                 }
  96.             } else {
  97.                 lmessage("Subject? ");
  98.                 gets(subject);
  99.             }
  100.  
  101.             lmessage("Distribution? ");
  102.             gets(dist);
  103.  
  104.             /* add the quoted message */
  105.             /* are we quoting? */
  106.             if (tx != NULL) {
  107.  
  108.                 get_his_stuff(tx, author, msg_id);
  109.                 message("Quote article (y/n)? ");
  110.                 while (((ch = tolower(getch())) != 'y') && (ch != 'n'));
  111.  
  112.                 if (ch == 'y') {
  113.  
  114.                     fprintf(article, "In article %s %s writes:\n", msg_id, author);
  115.                     ln = tx->start;
  116.                     while (ln != NULL) {
  117.                         fprintf(article, ">%s", ln->data);
  118.                         ln = ln->next;
  119.                     }
  120.                 }
  121.             }
  122.  
  123.             /* append the signature if there is one */
  124.             strcpy(sig_fn, my_stuff.home);
  125.             strcat(sig_fn, my_stuff.signature);
  126.             if ((sig = fopen(sig_fn, "rt")) != NULL) {
  127.                 fputs("\n--\n", article);
  128.                 while (fgets(buf, sizeof(buf)-1, sig) != NULL)
  129.                     fputs(buf, article);
  130.                 fclose(sig);
  131.             }
  132.  
  133.         fclose(article);
  134.  
  135.         ch = 'e';
  136.         while ((ch != 's') && (ch != 'a')) {
  137.             if (ch == 'e') {
  138.                sprintf(buf, my_stuff.editor, tempfile);
  139.                system(buf);
  140.             }
  141.             gotoxy(1,23);
  142. /*            cprintf("Article for newsgroup %s ", newsgroups); */
  143.             sprintf(buf,"   (S)end, (A)bort, or (E)dit again? (S/A/E):");
  144.             message(buf);
  145.             while (((ch = tolower(getch())) != 's') && (ch != 'a') && (ch != 'e'));
  146.             gotoxy(1,24);
  147.             }
  148.                 article = fopen(tempfile, "rt");
  149.                 if (ch == 's')
  150.                     post_it(article, newsgroups, subject, dist, msg_id);
  151.         }
  152.  
  153.         fclose(article);
  154.         unlink(tempfile);
  155.  
  156.     } else {
  157.         message("*** couldn't open temp article file - press any key ***");
  158.         get_any_key();
  159.     }
  160.  
  161. }
  162.  
  163.  
  164.  
  165. /*--------------------------- post an article ---------------------------*/
  166. void post_it(FILE *article, char *newsgroups, char *subject, char *dist,
  167.                   char *msg_id)
  168. {
  169.     /*
  170.      *  Post an article.  The article is passed as an open file,
  171.      *  with the other header data.  The header is added to the file
  172.      *  and the article sent.
  173.      */
  174.  
  175.     FILE   *tmp, *local, *log;
  176.     char   buf[256], *p;
  177.     char   ng[256];
  178.     char   remotegroups = 0;
  179.     char   d_name[20], x_name[20];
  180.     char   short_d_name[80], short_x_name[80];
  181.     int    ct, seq;
  182.     time_t t;
  183.     struct tm *gmt;
  184.     long   where;
  185.     ACTIVE *gp;
  186.  
  187.     static char *dow[] = {
  188.         "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  189.     };
  190.     static char *mth[] = {
  191.         "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
  192.         "Oct", "Nov", "Dec"
  193.     };
  194.  
  195.     ct = 0;
  196.  
  197.     seq = post_sequence();
  198.     sprintf(d_name, "D.%s%04x", my_stuff.my_site, seq);
  199.     sprintf(x_name, "X.%s%04x", my_stuff.my_site, seq);
  200.  
  201.     sprintf(short_d_name, "%sD_%04x.%.3s", my_stuff.temp_str, seq, my_stuff.my_site);
  202.     sprintf(short_x_name, "%sX_%04x.%.3s", my_stuff.temp_str, seq, my_stuff.my_site);
  203.  
  204.     /* count the lines */
  205.     rewind(article);
  206.     while (fgets(buf, sizeof(buf)-1, article) != NULL)
  207.         ct++;
  208.  
  209.  
  210.     /*
  211.      *  Make the final article file.  This posting stuff is a horrible
  212.      *  kludge (blush).  If you get around to fixing this, please
  213.      *  post me a copy :)
  214.      */
  215.     if ((tmp = fopen(short_d_name, "w+b")) != NULL) {
  216.  
  217.         /* see if any newsgroups are not local */
  218.         if (stricmp(dist, "local") != 0) {
  219.             strcpy(ng, newsgroups);
  220.             p = strtok(ng, " ,");
  221.             while (p != NULL) {
  222.                 if (!is_local_group(p)) {
  223.                     ++remotegroups;
  224.                     break;
  225.                 }
  226.                 p = strtok(NULL, " ,");
  227.             }
  228.         }
  229.  
  230.         fprintf(tmp, "Path: %s!%s\n", my_stuff.my_site, my_stuff.user);
  231.         fprintf(tmp, "From: %s@%s (%s)\n", my_stuff.user, my_stuff.my_domain,
  232.             my_stuff.my_name);
  233.         fprintf(tmp, "Newsgroups: %s\n", newsgroups);
  234.         fprintf(tmp, "Subject: %s\n", subject);
  235.         if (*dist)
  236.             fprintf(tmp, "Distribution: %s\n", dist);
  237.         fprintf(tmp, "Message-ID: <%ld.%dsnx@%s>\n", time(&t), seq, my_stuff.my_domain);
  238.         if (strlen(msg_id) > 0)
  239.             fprintf(tmp, "References: %s\n", msg_id);
  240.  
  241.         time(&t);
  242.         gmt = gmtime(&t);
  243.         fprintf(tmp, "Date: %s, %02d %s %02d %02d:%02d:%02d GMT\n",
  244.                 dow[gmt->tm_wday],
  245.                 gmt->tm_mday, mth[gmt->tm_mon], (gmt->tm_year % 100),
  246.                 gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
  247.  
  248.         if (*my_stuff.my_organisation)
  249.             fprintf(tmp, "Organization: %s\n", my_stuff.my_organisation);
  250.         if (stricmp (my_stuff.replyuser,"-none-") != 0) {
  251.             fprintf(tmp, "Reply-To: %s\n",my_stuff.replyuser);}
  252.         fprintf(tmp, "Lines: %d\n\n", ct);
  253.  
  254.         /* copy the rest */
  255.         rewind(article);
  256.         while (fgets(buf, sizeof(buf)-1, article) != NULL) {
  257.             fputs(buf, tmp);
  258.         }
  259.  
  260.         /* ok, now post it locally */
  261.         strcpy(ng, newsgroups);
  262.         p = strtok(ng, " ,");
  263.         while (p != NULL) {
  264.  
  265.             if (fseek(tmp, 0L, SEEK_SET) != 0) {
  266.                 fprintf(stderr, "cannot rewind temp file\n");
  267.                 exit(1);
  268.             }
  269.  
  270.             local = open_out_file(p);
  271.             where = ftell(local);
  272.             while (fgets(buf, sizeof(buf)-1, tmp) != NULL)  {
  273.                 fputs(buf, local);
  274.             }
  275.             fprintf(local, "\n@@@@END\n");
  276.             fclose(local);
  277.  
  278.             /* save the data in the index file */
  279.             local = open_index_file(p);
  280.             gp = find_news_group(p);
  281.             fprint